//
//  ZGLGoodsCollectionViewCell.m
//  Faker
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLGoodsCollectionViewCell.h"
#import "ZGLGoodsTableViewTitleCell.h"
#import "ZGLGoodsTableViewSelectCell.h"
#import "ZGLHTTPTOOL.h"
#import "ZGLSearchHeadUpModel.h"
#import "ZGLInformationNewsModel.h"
#import "ZGLOptionsModel.h"


#define ARC arc4random() % 1079



@interface ZGLGoodsCollectionViewCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableDictionary *dic;

@property (nonatomic, strong) UITableView *goodsTableView;
@property (nonatomic, strong) NSArray *idArr;
@property (nonatomic, strong) NSArray *introArr;

@property (nonatomic, strong) NSMutableArray *headArr;
@property (nonatomic, strong) NSMutableArray *leftArr;
@property (nonatomic, strong) NSMutableArray *rightArr;

@property (nonatomic, strong) NSDictionary *headDic;
@property (nonatomic, strong) NSDictionary *leftDic;
@property (nonatomic, strong) NSDictionary *rightDic;


@property (nonatomic, strong) NSMutableArray *arcArr;


@end

@implementation ZGLGoodsCollectionViewCell

- (void)dealloc {
    _goodsTableView.delegate = nil;
    _goodsTableView.dataSource = nil;
}

- (void)randomArray
{
    _arcArr = [NSMutableArray arrayWithCapacity:0];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
  
    
    //随机数从这里边产生
    NSMutableArray *startArray= [user arrayForKey:@"allOptions"].mutableCopy;
    for (NSInteger i = 0; i < 7; i++) {
        NSInteger arc = [[NSNumber numberWithInt:ARC] integerValue];
        ZGLOptionsModel *model = [ZGLOptionsModel modWithDic:startArray[arc]];
        [_arcArr addObject:model];
        [startArray removeObjectAtIndex:arc];
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _arcArr =[NSMutableArray arrayWithCapacity:0];
        [self randomArray];
        self.goodsTableView = [[UITableView alloc] initWithFrame:self.frame style:(UITableViewStylePlain)];
        self.goodsTableView.delegate = self;
        self.goodsTableView.dataSource = self;
        [self.contentView addSubview:self.goodsTableView];
        [self handleHeadData];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.backgroundColor = foreColor;
    [self.goodsTableView registerClass:[ZGLGoodsTableViewTitleCell class] forCellReuseIdentifier:@"ZGLGoodsCollectionViewCell_ZGLGoodsTableViewTitleCell"];
    [self.goodsTableView registerClass:[ZGLGoodsTableViewSelectCell class] forCellReuseIdentifier:@"ZGLGoodsCollectionViewCell_ZGLGoodsTableViewSelectCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 5;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ZGLGoodsTableViewTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLGoodsCollectionViewCell_ZGLGoodsTableViewTitleCell"];
        cell.headDic = self.headDic;
        cell.leftModel = _arcArr[0];
        cell.rightModel = _arcArr[1];
        [cell buttonBlock:^(UIButton *button) {
            if (button.tag == 120) {
                [self.delegate passHeadModelArr:_headArr];
            }
            if (button.tag == 121) {
                [self.delegate passModelArr:_arcArr[0]];
            }
            if (button.tag == 122) {
                [self.delegate passModelArr:_arcArr[1]];
            }
        }];
        return cell;
    } else {
        ZGLGoodsTableViewSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLGoodsCollectionViewCell_ZGLGoodsTableViewSelectCell"];
        cell.optionsModel = _arcArr[indexPath.row + 2];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGFLOAT_MIN;
    } else {
        return 20;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return ScaleY * 200;
    } else {
        return ScaleY * 105;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        [self.delegate passModelArr:_arcArr[indexPath.row + 2]];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(ScaleX * 20, 0, ScaleX * 100, ScaleY * 20)];
    
    view.backgroundColor = backColor;
    if (section == 1) {
        UILabel *label = [[UILabel alloc] init];
        [label setFrame:CGRectMake(ScaleX * 20, 0, ScaleX * 100, ScaleY * 20)];
        label.text = @"精选";
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:12];
        [view addSubview:label];
    }
    return view;
}
#pragma - mark headtitle
- (void)handleHeadData {
    [ZGLHTTPTOOL GETWithURL:@"http://iphone.myzaker.com/zaker/block_topic.php?_appid=AndroidPhone&_bsize=720_1280&_dev=31&_lat=38.889545&_lbs_city=%E5%A4%A7%E8%BF%9E&_lbs_province=%E8%BE%BD%E5%AE%81%E7%9C%81&_lng=121.551009&_mac=80%3A41%3A4e%3Aee%3Ae0%3A1f&_mcode=9B1587DA&_net=wifi&_nudid=6cfa9cc4da9a60ec&_os=4.3_vivoX3L&_os_name=vivoX3L&_udid=864510024231287&_v=6.4.1&_version=6.41&id=54dee8fa9490cbde430000ba" withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSMutableArray *arr = result[@"data"][@"list"];
        NSMutableArray *modelArr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in arr) {
            ZGLSearchHeadUpModel *model = [ZGLSearchHeadUpModel modWithDic:dic];
            [modelArr addObject:model];
        }
        _headArr = modelArr;
        self.headDic = result[@"data"][@"head"];
        [self.goodsTableView reloadData];
    } withFail:^(id result) {
        
    }];
}



@end
